/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tr.com.oyunus.server.impl;

import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tr.com.oyunus.server.OyunusServer;

import javax.annotation.PostConstruct;
import java.net.InetSocketAddress;
import java.util.concurrent.Executors;


/**
 * This is the startup class of server.
 *
 * @author ginnun
 */
@Component
public class SimpleOyunusServer implements OyunusServer {
    private static final Logger logger = LoggerFactory.getLogger(SimpleOyunusServer.class);

    @Autowired
    SimpleOyunusPipelineFactory pipelineFactory;

    @PostConstruct                    // this postconstructer needs to be in a beanfactory.
    @Override
    public void startServer() {
        logger.info("Oyunus Game Server {} starting... ", "v01"); // @TODO versiyon numarası generic bir yerden gelsin.

        ServerBootstrap bootstrap = new ServerBootstrap(
                new NioServerSocketChannelFactory(
                        Executors.newCachedThreadPool(),
                        Executors.newCachedThreadPool()));

        bootstrap.setPipelineFactory(pipelineFactory);
        // Bind and start to accept incoming connections.
        bootstrap.bind(new InetSocketAddress(8080)); // @todo port numarasıda generic bir yerden gelmeli

        bootstrap.setOption("child.keepAlive", true); // for mobiles & our stateful app
        bootstrap.setOption("child.tcpNoDelay", true); // better latency over bandwidth
        bootstrap.setOption("reuseAddress", true);// kernel optimization
        bootstrap.setOption("child.reuseAddress", true); // kernel optimization

        logger.info("\t******************************");
        logger.info("\t****Oyunus Game Server v01****");
        logger.info("\t******************************");
        logger.info("Listening on localhost:8080 ...");

    }
}
